<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>轮播图编辑</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/frame.css">

    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>
<body style="background-color: rgb(255, 255, 255);">           
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>轮播图编辑</legend>
</fieldset>

<form class="layui-form layui-form-pane" action="" lay-filter="example" style="padding: 20px">
    <input type="hidden" name="id" lay-verify="id" value="{$info.id}">

    <div class="layui-form-item" style="display: flex;">
        <label class="layui-form-label">缩略图</label>
        <div class="layui-input-inline">
            <div class="fup_files">
                <input type="file" name="img" onchange="fuploads($(this))">
                <img src="{$info.img}" class="img" name="img" width="100%" height="100%"/>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
            <a href="javascript:history.back(-1)" class="layui-btn layui-btn-primary">返回</a>
        </div>
    </div>
</form>
          
<script src="/static/layui/layui.js" charset="utf-8"></script>

<script>
    // 图片上传
    function fuploads(f){
        var nameAll = f[0].files[0].type;
        var name = nameAll.split("/");
        
        if(name[0] != 'image'){
            layer.msg('上传类型错误', {icon: 2});
        }else{
            var url = window.URL.createObjectURL(f[0].files[0]);  // 得到bolb对象路径，可当成普通的文件路径一样使用
            f.next("img").css('display', 'block');
            f.next("img").attr('src', url);
        }
    }

    layui.use(['form'], function(){
        var form = layui.form, layer = layui.layer;

        form.on('submit(submit)', function(data){
            let formData = new FormData();
            let img = $("input[name='img");

            formData.append('id', data.field.id);
            formData.append('img', img[0].files[0]);

            // 加载中样式... 
            let loading = layer.msg('正在修改，请稍后...', {icon: 16, shade: 0.3, time:0});

            $.ajax({
                url:'{:url("Set/banner_edit_data")}',
                data:formData,
                contentType: false,
                processData: false,
                type:"post",
                dataType:'json',
                success:function(res){
                    layer.close(loading);
                    if(res.code == 0){
                        layer.msg(res.msg, {icon: 1,time: 1500}, function(){
                            history.back(-1);
                        });
                    }else{
                        layer.msg(res.msg, {icon: 2});
                    }
                },
                error:function(res){
                    layer.msg(res.msg, {icon: 2});
                }
            })

            return false;
        });
    });
</script>
</body>
</html>